/*
Copyright (C) 1996-1997 Id Software, Inc.

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 3
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

*/

#include "fxVSIncludes.fx"

struct PS_NEWLIGHTMAPPED
{
	float4 Position : SV_POSITION;
	float2 TexCoord : TEXCOORD;
	float3 Lightmap : LIGHTMAP;
	nointerpolation float4 Styles : STYLES;
};

struct PS_UNLIT
{
	float4 Position : SV_POSITION;
	float2 TexCoord : TEXCOORD;
};


#ifdef VERTEXSHADER
float4 SurfZFillVS (SURF_VS vs_in) : SV_POSITION
{
	return mul (EntityMatrix, mul (LocalMatrix, vs_in.Position));
}

PS_UNLIT SurfUnlitVS (SURF_VS vs_in)
{
	PS_UNLIT vs_out;

	vs_out.Position = mul (EntityMatrix, mul (LocalMatrix, vs_in.Position));
	vs_out.TexCoord = vs_in.TexCoord;

	return vs_out;
}

PS_NEWLIGHTMAPPED SurfLightmappedVS (SURF_VS vs_in)
{
	PS_NEWLIGHTMAPPED vs_out;

	vs_out.Position = mul (EntityMatrix, mul (LocalMatrix, vs_in.Position));
	vs_out.TexCoord = vs_in.TexCoord;
	vs_out.Lightmap = float3 (vs_in.Lightmap, vs_in.MapNum);

	// load the lightstyles
	vs_out.Styles = float4 (
		LightStyleValues[vs_in.Styles.x],
		LightStyleValues[vs_in.Styles.y],
		LightStyleValues[vs_in.Styles.z],
		LightStyleValues[vs_in.Styles.w]
	);

	return vs_out;
}
#endif


#ifdef PIXELSHADER
void SurfZFillPS () {}

// bind the lightmap textures from slots 10 onwards so that they won't be overwritten and need only be bound once per map
Texture2DArray lmap0Texture : register(TEXTURE_SLOT_LIGHTMAP0);
Texture2DArray lmap1Texture : register(TEXTURE_SLOT_LIGHTMAP1);
Texture2DArray lmap2Texture : register(TEXTURE_SLOT_LIGHTMAP2);
Texture2DArray lmap3Texture : register(TEXTURE_SLOT_LIGHTMAP3);

float4 SurfGetLightmap (Texture2DArray lmapTexture, float3 TexCoord, float Style)
{
	float4 lmap = lmapTexture.Sample (lmapSampler, TexCoord);
	return (lmap / lmap.a) * Style;
}

float4 SurfUnlitPS (PS_UNLIT ps_in) : SV_TARGET0
{
	return surfTexture.Sample (surfSampler, ps_in.TexCoord);
}

float4 SurfLightmapped0PS (PS_NEWLIGHTMAPPED ps_in) : SV_TARGET0
{
	float4 diff = surfTexture.Sample (surfSampler, ps_in.TexCoord);
	float4 lmap = SurfGetLightmap (lmap0Texture, ps_in.Lightmap, ps_in.Styles.x);

	return float4 (diff.rgb * lmap.rgb, Alpha);
}

float4 SurfLightmapped1PS (PS_NEWLIGHTMAPPED ps_in) : SV_TARGET0
{
	float4 diff = surfTexture.Sample (surfSampler, ps_in.TexCoord);

	float4 lmap = SurfGetLightmap (lmap0Texture, ps_in.Lightmap, ps_in.Styles.x) +
				  SurfGetLightmap (lmap1Texture, ps_in.Lightmap, ps_in.Styles.y);

	return float4 (diff.rgb * lmap.rgb, Alpha);
}

float4 SurfLightmapped2PS (PS_NEWLIGHTMAPPED ps_in) : SV_TARGET0
{
	float4 diff = surfTexture.Sample (surfSampler, ps_in.TexCoord);

	float4 lmap = SurfGetLightmap (lmap0Texture, ps_in.Lightmap, ps_in.Styles.x) +
				  SurfGetLightmap (lmap1Texture, ps_in.Lightmap, ps_in.Styles.y) +
				  SurfGetLightmap (lmap2Texture, ps_in.Lightmap, ps_in.Styles.z);

	return float4 (diff.rgb * lmap.rgb, Alpha);
}

float4 SurfLightmapped3PS (PS_NEWLIGHTMAPPED ps_in) : SV_TARGET0
{
	float4 diff = surfTexture.Sample (surfSampler, ps_in.TexCoord);

	float4 lmap = SurfGetLightmap (lmap0Texture, ps_in.Lightmap, ps_in.Styles.x) +
				  SurfGetLightmap (lmap1Texture, ps_in.Lightmap, ps_in.Styles.y) +
				  SurfGetLightmap (lmap2Texture, ps_in.Lightmap, ps_in.Styles.z) +
				  SurfGetLightmap (lmap3Texture, ps_in.Lightmap, ps_in.Styles.w);

	return float4 (diff.rgb * lmap.rgb, Alpha);
}
#endif

